//package com.examination.client.config;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.examination.client.domian.dto.UserLoginDTO;
//import com.examination.client.entity.CourierSite;
//import com.examination.client.entity.UserInfo;
//import com.examination.client.enums.SiteTypeEnum;
//import com.examination.client.service.ICourierSiteService;
//import com.examination.client.service.IUserInfoService;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.util.ByteSource;
//import org.springframework.beans.BeanUtils;
//
//import javax.annotation.Resource;
//import java.util.Objects;
//
///**
// * @Description:
// * @Auther: xiaddong
// * @Date: 2021/10/20
// */
//public class ShiroDbRealm extends AuthorizingRealm {
//
//    @Resource
//    private IUserInfoService userInfoService;
//
//    @Resource
//    ICourierSiteService courierSiteService;
//
//
//
//    /**
//     * 授权
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        UserLoginDTO user = (UserLoginDTO) principals.getPrimaryPrincipal();
//        return info;
//    }
//    /**
//     * 登录认证
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationtoken) throws AuthenticationException {
//        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationtoken;
//        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(UserInfo::getUserName,usernamePasswordToken.getUsername());
//        UserInfo userInfo = userInfoService.getOne(queryWrapper);
//
//        if (userInfo == null){
////            throw new CredentialsException();
//            userInfo = new UserInfo();
//            userInfo.setSiteId(1);
//        }
//        CourierSite courierSite = courierSiteService.getById(userInfo.getSiteId());
//
//        if(Objects.isNull(courierSite)){
//            throw new CredentialsException();
//        }
//
//
//        UserLoginDTO shiroUserDTO = new UserLoginDTO();
//        BeanUtils.copyProperties(userInfo,shiroUserDTO);
//        SimpleAuthenticationInfo authinfo = new SimpleAuthenticationInfo(shiroUserDTO,
//                userInfo.getPassword(), ByteSource.Util.bytes(userInfo.getSalt()), getName());
//        return authinfo;
//    }
//
//}
